package com.jxc.dao.impl;

import java.sql.SQLException;
import java.util.List;




import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jxc.dao.InquiryDao;
import com.jxc.entity.Inquiry;
import com.jxc.util.PageBean;

public class InquiryDaoImpl extends HibernateDaoSupport implements InquiryDao {

	public void deleteInquiry(Inquiry inquiry) {
      this.getHibernateTemplate().delete(inquiry);
	}

	public Inquiry findInquiry(Inquiry inquiry) {
		List<Inquiry> list=this.getHibernateTemplate().find("from Inquiry inquiry where inquiry.sn=?",inquiry.getSn());
		if(list!=null&&list.size()>0)
			list.get(0);
		return null;
	}
    
	public PageBean findInquiryList(final String hql, final PageBean bean) {
		this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				  Query query=session.createQuery(hql);
				  bean.setTotalitem(query.list().size());
				  query.setFirstResult((bean.getPageon()-1)*bean.getPagesize());
				  query.setMaxResults(bean.getPagesize());
				  bean.getTotalpage();
				  bean.setList(query.list());
				return bean;
			}
		});
		return bean;
	}

	public void insertInquiry(Inquiry inquiry) {
      this.getHibernateTemplate().saveOrUpdate(inquiry);
	}

	public void updateInquiry(Inquiry inquiry) {

		this.getHibernateTemplate().update(inquiry);
	}

	public Inquiry findInquiryById(int inquiryId) {
		
		return (Inquiry)this.getHibernateTemplate().get(Inquiry.class,inquiryId);
	}

}
